package com.leapp.sk.ui.activity

import android.os.Bundle
import android.view.KeyEvent
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.bean.MainTabObj
import com.leapp.sk.binder.MaintableDataBinder
import com.example.util.inter.MsgListUpdateInterface
import com.leapp.sk.util.ExamineInterceptUtils
import com.example.util.im.MessageListPresenter
import com.leapp.sk.viewdelegate.MainTableDelegate

/**
 * 主页框架
 * @author  chaohaipeng  on 2018/7/2.
 */
class AppMainTableActivity : BaseActivityMVP<MainTableDelegate>(), MsgListUpdateInterface {
    /**
     * 监听返回键，实现双击返回桌面
     */
    private var firstTime: Long? = 0
    override fun updateMsg(message: Any) {
        viewDelegate.loadConversations()
    }

    /**
     * 消息观察者
     */
    var messagePresenter: MessageListPresenter?=null

    override fun initDate() {
        ExamineInterceptUtils.getInstance().initDialog(this)
        messagePresenter= MessageListPresenter()
        messagePresenter?.setmsgUpdateInterfaceListener(this)
        messagePresenter?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        messagePresenter?.stopMessagePresenter()
    }
    override fun getDelegateClass(): Class<MainTableDelegate> {
        return MainTableDelegate::class.java
    }
    override fun getDataBinder(): BaseDataBinder<*, *> {
        return MaintableDataBinder()
    }
    override fun initView(p0: Bundle?) {
        notifyModelChanged(MainTabObj())
    }
    override fun bindEvenListener() {

    }
    override fun onResume() {
        super.onResume()
        viewDelegate.loadConversations()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (firstTime != null && System.currentTimeMillis() - firstTime!! < 2000) {
                finish()
                return true
            }
            LKToastUtil.showToastShort( resources.getString(R.string.app_exit_window))
            firstTime = System.currentTimeMillis()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

}
